//路由  集成
import React, {Component} from 'react'
import {BackHandler, DeviceEventEmitter, View, Text, StyleSheet } from 'react-native'
import {addNavigationHelpers, NavigationActions} from 'react-navigation'
import {
  createReduxBoundAddListener,
  createReactNavigationReduxMiddleware
} from 'react-navigation-redux-helpers';
import {connect} from 'react-redux'
import {ActionTypes} from '../constants'

import StacksOverTabs from './StacksOverTabs'
export const AppNavigator = StacksOverTabs;

const mapStateToProps = (state) => ({
  nav: state.nav
});

const middleware = createReactNavigationReduxMiddleware(
  "root",
  state => state.nav,
);

const addListener = createReduxBoundAddListener("root");

class AppRouter extends React.Component {

  constructor(props) {
    super(props)
    this.state = {
    }
  }

  componentDidMount() {

  }

  componentWillUnmount() {
    BackHandler.removeEventListener("hardwareBackPress", this.onBackPress)
  }

  componentDidUpdate(preProps) {
  }

  onBackPress = () => {
    const {dispatch, nav} = this.props
    if (nav.index === 0) {
      BackHandler.exitApp();
      return false
    }
    dispatch(NavigationActions.back())
    return true
  }

  render() {
    const navigation = addNavigationHelpers({dispatch: this.props.dispatch, state: this.props.nav, addListener})
    return (
      <View style={styles.container}>
        <AppNavigator navigation={navigation}/>
      </View>
    )
  }

}

const styles = StyleSheet.create({
  container: {
    flex: 1,
  },
});


export const AppWithNavigationState = connect(mapStateToProps)(AppRouter)
